{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## model.modules()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sequential(\n",
      "  (0): Linear(in_features=10, out_features=20, bias=True)\n",
      "  (1): ReLU()\n",
      "  (2): Sequential(\n",
      "    (0): Conv2d(1, 3, kernel_size=(3, 3), stride=(1, 1))\n",
      "    (1): Sigmoid()\n",
      "  )\n",
      ")\n",
      "Linear(in_features=10, out_features=20, bias=True)\n",
      "ReLU()\n",
      "Sequential(\n",
      "  (0): Conv2d(1, 3, kernel_size=(3, 3), stride=(1, 1))\n",
      "  (1): Sigmoid()\n",
      ")\n",
      "Conv2d(1, 3, kernel_size=(3, 3), stride=(1, 1))\n",
      "Sigmoid()\n"
     ]
    }
   ],
   "source": [
    "import torch.nn as nn\n",
    "\n",
    "model = nn.Sequential(\n",
    "    nn.Linear(10, 20),\n",
    "    nn.ReLU(),\n",
    "    nn.Sequential(\n",
    "        nn.Conv2d(1, 3, 3),\n",
    "        nn.Sigmoid()\n",
    "    )\n",
    ")\n",
    "\n",
    "for module in model.modules():\n",
    "    print(module)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## model.named_modules()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " Sequential(\n",
      "  (0): Linear(in_features=10, out_features=20, bias=True)\n",
      "  (1): ReLU()\n",
      "  (2): Sequential(\n",
      "    (0): Conv2d(1, 3, kernel_size=(3, 3), stride=(1, 1))\n",
      "    (1): Sigmoid()\n",
      "  )\n",
      ")\n",
      "0 Linear(in_features=10, out_features=20, bias=True)\n",
      "1 ReLU()\n",
      "2 Sequential(\n",
      "  (0): Conv2d(1, 3, kernel_size=(3, 3), stride=(1, 1))\n",
      "  (1): Sigmoid()\n",
      ")\n",
      "2.0 Conv2d(1, 3, kernel_size=(3, 3), stride=(1, 1))\n",
      "2.1 Sigmoid()\n"
     ]
    }
   ],
   "source": [
    "for name, module in model.named_modules():\n",
    "    print(name, module)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## model.children()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Linear(in_features=10, out_features=20, bias=True)\n",
      "ReLU()\n",
      "Sequential(\n",
      "  (0): Conv2d(1, 3, kernel_size=(3, 3), stride=(1, 1))\n",
      "  (1): Sigmoid()\n",
      ")\n"
     ]
    }
   ],
   "source": [
    "for child in model.children():\n",
    "    print(child)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## model.named_children()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 Linear(in_features=10, out_features=20, bias=True)\n",
      "1 ReLU()\n",
      "2 Sequential(\n",
      "  (0): Conv2d(1, 3, kernel_size=(3, 3), stride=(1, 1))\n",
      "  (1): Sigmoid()\n",
      ")\n"
     ]
    }
   ],
   "source": [
    "for name, child in model.named_children():\n",
    "    print(name, child)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## model.parameters()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([10, 1, 5, 5])\n",
      "torch.Size([10])\n",
      "torch.Size([50, 320])\n",
      "torch.Size([50])\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "\n",
    "# 定义一个简单的模型\n",
    "class SimpleModel(nn.Module):\n",
    "    def __init__(self):\n",
    "        super(SimpleModel, self).__init__()\n",
    "        self.conv1 = nn.Conv2d(1, 10, kernel_size=5)\n",
    "        self.fc1 = nn.Linear(320, 50)\n",
    "\n",
    "    def forward(self, x):\n",
    "        # 前向传播逻辑\n",
    "        pass\n",
    "\n",
    "# 实例化模型\n",
    "model = SimpleModel()\n",
    "\n",
    "# 使用 parameters() 遍历模型参数\n",
    "for param in model.parameters():\n",
    "    print(param.size())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total trainable parameters:  16310\n"
     ]
    }
   ],
   "source": [
    "total_trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)\n",
    "print(\"Total trainable parameters: \", total_trainable_params)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## model.named_parameters()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "conv1.weight torch.Size([10, 1, 5, 5])\n",
      "conv1.bias torch.Size([10])\n",
      "conv2.weight torch.Size([20, 10, 5, 5])\n",
      "conv2.bias torch.Size([20])\n",
      "fc1.weight torch.Size([50, 320])\n",
      "fc1.bias torch.Size([50])\n",
      "fc2.weight torch.Size([10, 50])\n",
      "fc2.bias torch.Size([10])\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "\n",
    "# 定义一个简单的模型\n",
    "class SimpleModel(nn.Module):\n",
    "    def __init__(self):\n",
    "        super(SimpleModel, self).__init__()\n",
    "        self.conv1 = nn.Conv2d(1, 10, kernel_size=5)\n",
    "        self.conv2 = nn.Conv2d(10, 20, kernel_size=5)\n",
    "        self.fc1 = nn.Linear(320, 50)\n",
    "        self.fc2 = nn.Linear(50, 10)\n",
    "\n",
    "    def forward(self, x):\n",
    "        # 前向传播逻辑\n",
    "        pass\n",
    "\n",
    "# 实例化模型\n",
    "model = SimpleModel()\n",
    "\n",
    "# 使用 named_parameters() 遍历模型参数\n",
    "for name, param in model.named_parameters():\n",
    "    print(name, param.size())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## model.state_dict()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "conv1.weight torch.Size([10, 1, 5, 5])\n",
      "conv1.bias torch.Size([10])\n",
      "conv2.weight torch.Size([20, 10, 5, 5])\n",
      "conv2.bias torch.Size([20])\n",
      "fc1.weight torch.Size([50, 320])\n",
      "fc1.bias torch.Size([50])\n",
      "fc2.weight torch.Size([10, 50])\n",
      "fc2.bias torch.Size([10])\n"
     ]
    }
   ],
   "source": [
    "state_dict = model.state_dict()\n",
    "for key, value in state_dict.items():\n",
    "    print(key, value.size())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "torch",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
